﻿using System.Collections.Generic;
using System.Linq;
using NthDownload.Common.Plugins;
using NthDownload.Common.ViewModels;
using NthDownload.BL;
using System.ComponentModel.Composition;
using NthDownload.Contracts.Plugins.BandwidthProviders;
using NthDownload.Composition;
using System.Collections;
using NthDownload.Contracts;
using System;
using NthDownload.Common.Utils;

namespace NthDownload.ViewModels
{
    public class BandwidthProvidersViewModel : ViewModelScreenBase
    {
        #region Class Members

        private NthProfile _profile;
        private IList _selectedBandwidthProviders;
        private double _vereticalOffset;
        private double _horizontalOffset;

        #endregion Class Members

        #region Properties

        /// <summary>
        /// The currently selected bandwidth providers
        /// </summary>
        public IList SelectedBandwidthProviders
        {
            get
            {
                return _selectedBandwidthProviders;
            }
            set
            {
                _selectedBandwidthProviders = value;
                NotifyOfPropertyChange(() => SelectedBandwidthProviders);
            }
        }

        /// <summary>
        /// The vertical offset of the list
        /// </summary>
        public double VerticalOffset
        {
            get
            {
                return _vereticalOffset;
            }
            set
            {
                _vereticalOffset = value;
                NotifyOfPropertyChange(() => VerticalOffset);
            }
        }

        /// <summary>
        /// The horizontal offset of the list
        /// </summary>
        public double HorizontalOffset
        {
            get
            {
                return _horizontalOffset;
            }
            set
            {
                _horizontalOffset = value;
                NotifyOfPropertyChange(() => HorizontalOffset);
            }
        }

        /// <summary>
        /// The profile
        /// </summary>
        [Import(AllowRecomposition = true)]
        public NthProfile Profile
        {
            get
            {
                return _profile;
            }
            set
            {
                _profile = value;
                NotifyOfPropertyChange(() => Profile);
            }
        }

        #endregion Properties

        #region Methods

        /// <summary>
        /// Adds a new bandwidth provider
        /// </summary>
        public void AddNewBandwidthProvider()
        {
            //
            // Show the bandwidth provider plugin selector
            //
            BandwidthProviderPluginSelectorViewModel bandwidthProviderPluginSelectorViewModel = new BandwidthProviderPluginSelectorViewModel();
            if (NavigationService.ShowDialog(bandwidthProviderPluginSelectorViewModel, false) != Contracts.ViewModelResults.Select || bandwidthProviderPluginSelectorViewModel.SelectedPlugin == null)
            {
                return;
            }

            //
            // Get bandwidth provider plugin
            //
            IBandwidthProviderUIService bandwidthProviderUIService;
            if (!ImportService.TryGetExportValue(BandwidthProviderPluginContractNameService.CreateBandwidthProviderUIServiceContractName(bandwidthProviderPluginSelectorViewModel.SelectedPlugin.GUID), out bandwidthProviderUIService))
            {
                MessageDialogViewModel.ShowError("Couldn't load the UI Service from the " + bandwidthProviderPluginSelectorViewModel.SelectedPlugin.DisplayName + " plugin.");
                return;
            }

            //
            // Create bandwidth povider
            //
            IBandwidthProvider bandwidthProvider; 
            if (bandwidthProviderUIService.NewBandwidthProvider(out bandwidthProvider) == Contracts.ViewModelResults.Save && bandwidthProvider != null)
            {
                Profile.AddBandwidthProvider(bandwidthProvider);
            }
        }

        /// <summary>
        /// Edits the selected bandwidth provider
        /// </summary>
        public void EditBandwidthProvider(IBandwidthProvider bandwidthProvider)
        {
            //
            // Get bandwidth provider plugin
            //
            IBandwidthProviderUIService bandwidthProviderUIService;
            if (!ImportService.TryGetExportValue(BandwidthProviderPluginContractNameService.CreateBandwidthProviderUIServiceContractName(bandwidthProvider.ParentGUID), out bandwidthProviderUIService))
            {
                MessageDialogViewModel.ShowError("Couldn't load the UI Service from the bandwidth provider plugin.");
                return;
            }

            //
            // Edit the bandwidth provider
            //
            bandwidthProviderUIService.EditBandwidthProvider(bandwidthProvider);
        }

        /// <summary>
        /// Removes the selected bandwidth providers
        /// </summary>
        public void RemoveBandwidthProviders(IBandwidthProvider bandwidthProvider)
        {
            MessageDialogViewModel messageDialogViewModel = new MessageDialogViewModel("Are you sure you want to delete the '" + bandwidthProvider.Name + "' bandwidth provider.", ViewModelResults.Yes)
            {
                ShowYes = true,
                ShowNo = true,
            };

            if (NavigationService.ShowDialog(messageDialogViewModel, false) != ViewModelResults.Yes)
            {
                return;
            }

            try
            {
                //
                // If the item is selected then remove all selected items otherwise only removed the passed item
                //
                if (SelectedBandwidthProviders != null && SelectedBandwidthProviders.Contains(bandwidthProvider))
                {
                    List<IBandwidthProvider> selectedBandwidthProviders = SelectedBandwidthProviders.OfType<IBandwidthProvider>().ToList();
                    foreach (IBandwidthProvider item in selectedBandwidthProviders)
                    {
                        Profile.RemoveBandwidthProvider(item);
                    }
                }
                else
                {
                    Profile.RemoveBandwidthProvider(bandwidthProvider);
                }
            }
            catch (Exception e)
            {
                MessageDialogViewModel.ShowError(ExceptionUtils.GetInnerMostException(e).Message);
            }
        }

        #endregion Methods
    }
}
